package kr.co.webmill;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ContactPickerActivity extends Activity {
    /** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Intent intent = getIntent();
		//String dataPath = intent.getData().toString();

		//final Uri data = Uri.parse(dataPath + "people/");
		final Uri data = ContactsContract.Contacts.CONTENT_URI;
		final Cursor c = managedQuery(data, null, null, null, null);
		String[] from = new String[]{ContactsContract.Contacts.DISPLAY_NAME};
		int[] to = new int[]{ R.id.itemTextView };

		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
				R.layout.listitemlayout,
				c,
				from,
				to);
		ListView lv = (ListView) findViewById(R.id.contactlistView);
		lv.setOnItemClickListener( new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int pos,
					long id) {
				// TODO Auto-generated method stub
				c.moveToPosition(pos);
				int rowId = c.getInt(c.getColumnIndexOrThrow("_id"));
				Uri outUrl = Uri.parse(data.toString() + rowId);
				
				Intent outData = new Intent();
				outData.setData(outUrl);
				setResult(Activity.RESULT_OK, outData);
				
			}
			
		});


	}

}